package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1577 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int numTriplets(int[] nums1, int[] nums2) {
            Map<Long, Integer> map1 = new HashMap<>();
            Map<Long, Integer> map2 = new HashMap<>();
            for (int i = 0; i < nums1.length - 1; i++) {
                for (int j = i + 1; j < nums1.length; j++) {
                    long product = (long) (nums1[i]) * (long) (nums1[j]);
                    map1.put(product, map1.getOrDefault(product, 0) + 1);
                }
            }
            for (int i = 0; i < nums2.length - 1; i++) {
                for (int j = i + 1; j < nums2.length; j++) {
                    long product = (long) (nums2[i]) * (long) (nums2[j]);
                    map2.put(product, map2.getOrDefault(product, 0) + 1);
                }
            }
            return calculate(map1, nums2) + calculate(map2, nums1);
        }

        public int calculate(Map<Long, Integer> map, int[] nums) {
            int count = 0;
            for (int i = 0; i < nums.length; i++) {
                long product = (long) nums[i] * (long) nums[i];
                if (map.containsKey(product)) {
                    count += map.get(product);
                }
            }
            return count;
        }
    }


    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC1577 an = new LC1577();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
